﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace AdverseEventReportSystem.Web.Api
{
    /// <summary>
    /// 不良事件接口
    /// </summary>
    public class AdverseEventController : ApiController
    {
        #region 获取事件类型

        /// <summary>
        /// 获取事件类型
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage GetAdverseEventTypeList()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                DataTable dt = AdverseEventTypeService.GetAdverseEventTypeTable();
                resultInfo.Body = dt;
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }

        #endregion

        #region 根据事件类型获取发生方式

        /// <summary>
        /// 根据事件类型获取发生方式
        /// </summary>
        /// <param name="typeID"></param>
        /// <param name="eventID"></param>
        /// <returns></returns>
        public HttpResponseMessage GetAdverseEventModeList(int typeID, int eventID = 0)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                DataTable dt = new DataTable();
                if (eventID == 0)
                {
                    dt = AdverseEventModeService.GetAdverseEventModeTable(typeID);
                }
                else
                {
                    dt = AdverseEventModeRecordService.GetAdverseEventModeRecordTable(eventID, typeID);
                }
                resultInfo.Body = dt;
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }

        #endregion

        #region 根据事件类型获取根因

        /// <summary>
        /// 根据事件类型获取根因
        /// </summary>
        /// <param name="typeID"></param>
        /// <param name="eventID"></param>
        /// <returns></returns>
        public HttpResponseMessage GetAdverseEventReasonList(int typeID, int eventID = 0)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                DataTable dt = new DataTable();
                if (eventID == 0)
                {
                    dt = AdverseEventReasonService.GetAdverseEventReasonTable(typeID);
                }
                else
                {
                    dt = AdverseEventReasonRecordService.GetAdverseEventReasonRecordTable(eventID, typeID);
                }
                resultInfo.Body = dt;
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }

        #endregion

        #region 添加/编辑不良事件

        /// <summary>
        /// 添加/编辑不良事件
        /// </summary>
        /// <param name="adverseEventInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Save([FromBody]string adverseEventInfoJson)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                AdverseEventInfo adverseEventInfo = js.Deserialize<AdverseEventInfo>(adverseEventInfoJson);
                adverseEventInfo.AdverseEvent.DataSource = 1;
                resultInfo = AdverseEventManager.Save(adverseEventInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return JsonDate(resultInfo);
        }

        #endregion

        #region 返回处理过的时间的Json字符串

        /// <summary>
        /// 返回处理过的时间的Json字符串
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public HttpResponseMessage JsonDate(object date)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string str = JsonConvert.SerializeObject(date, Formatting.Indented, timeConverter);
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }

        #endregion
    }
}
